package com.zz.parkingsystem.controller.inspector;
import com.zz.parkingsystem.entity.Checkon;
import com.zz.parkingsystem.entity.Inspector;
import com.zz.parkingsystem.service.CheckOnService;
import com.zz.parkingsystem.service.InspectorService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.MapVo;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
   已测updateCheckOn(上班打卡已测试)
   已测selCheck(考勤统计已测试)

 */
@RestController
@RequestMapping("/ins/check")
public class InsCheckonController {
    @Resource
    private CheckOnService checkOnService;
    @Resource
    private InspectorService inspectorService;
    @PostMapping("/updateCheckOn")  //巡检员上班打卡，传进来巡检的实体，通过巡检id去新增考勤表记录
    public Result update(HttpSession session){
        Inspector inspector1 = (Inspector)session.getAttribute("inspector");
        //先查出来巡检(测试用：后面从session中取)  ？？？？？？？？？？？？？？？？？？？？？
        Inspector inspector = inspectorService.getselectByPrimaryTel("17629074693");
        //在该巡检对应的考勤表中插入打卡记录
        Checkon checkon = new Checkon();
        checkon.setCoTime(new Date());
        checkon.setCoStatus(0);
        checkon.setInsId(inspector.getInsId());
        int i = checkOnService.insertSelective(checkon);
        if(i>0){
            return ResultGenerator.genSuccessResult("打卡成功");
        }
        return ResultGenerator.genFailResult("录入失败");
    }
    @PostMapping("/selCheck/{time}")//考勤记录页面传入时间，返回页面显示数据
    public Result selCheck(HttpSession session,@PathVariable("time") String time) throws ParseException {
        Inspector inspector = (Inspector) session.getAttribute("inspector");
        //将页面传来的时间string类型转换格式
        SimpleDateFormat sdf = new SimpleDateFormat(("yyyy-MM-dd"));
        java.util.Date time2=null;
        time2 = sdf.parse(time);
        java.sql.Date date = new java.sql.Date(time2.getTime());
        //传入时间查考勤表每天打卡情况,sql模糊查(返回给前端)
        // (inspector.getInsId()后面从session中取|| 7是测试时候手动模拟的) ？？？？？？？？？？？？？？？？
        List checkon = checkOnService.getselectByPrimaryKeyBytime(date,7);
        //正常上班天数(返回给前端)
        // (inspector.getInsId()后面从session中取|| 7是测试时候手动模拟的) ？？？？？？？？？？？？？？？？
        int i1 = checkOnService.getselectByPrimaryKeyBytimeNormal(date,7);
        //迟到上班天数(返回给前端)
        // (inspector.getInsId()后面从session中取|| 7是测试时候手动模拟的) ？？？？？？？？？？？？？？？？
        int i2 = checkOnService.getselectByPrimaryKeyBytimeLate(date,7);
//        Map<String, Object> map =new HashMap<>();
//        map.put("checkon",checkon);
//        map.put("i1",i1);
//        map.put("i2",i2);
        MapVo mapVo = new MapVo();
        mapVo.setI1(i1);
        System.out.println("正常上班天数=============="+i1);
        mapVo.setI2(i2);
        System.out.println("异常上班天数=============="+i2);
        return ResultGenerator.genSuccessResult(mapVo);
    }

}
